package at.fhj.mobsecdev.cookit.activities;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import at.fhj.mobsecdev.cookit.R;
import at.fhj.mobsecdev.cookit.model.Recipe;
import at.fhj.mobsecdev.cookit.util.Constants;
import at.fhj.mobsecdev.cookit.webrequests.GetImageWebCall;
import at.fhj.mobsecdev.cookit.webrequests.ViewRecipesAsyncWebCall;
import at.fhj.mobsecdev.cookit.webrequests.ViewRecipesAsyncWebCall.AddRecipes;
import at.fhj.mobsecdev.cookit.webrequests.WebServiceUrls;

/**
 * Activity that displays the details for a single recipe 
 */
public class ViewRecipeActivity extends Activity implements AddRecipes {

	private Recipe associatedRecipe;

	private TextView recipeName;
	private TextView isVegetarian;
	private TextView ingredients;
	private TextView preparation;
	private ImageView image;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_recipe);

		/* Associate controls with content view */
		recipeName = (TextView)findViewById(R.id.recipeName);
		isVegetarian = (TextView)findViewById(R.id.isVegetarian);
		ingredients = (TextView)findViewById(R.id.ingredients);
		preparation = (TextView)findViewById(R.id.preparation);
		image = (ImageView)findViewById(R.id.image);
		
		Bundle extras = getIntent().getExtras();
		
		/* This view was called by either selecting a recipe in the list or by selecting "Recipe of the Day" */
		if(extras.containsKey(Constants.DISPLAY_SELECTED_RECIPE))
		{
			setAssociatedRecipe((Recipe)extras.get(Constants.DISPLAY_SELECTED_RECIPE));
		}
		else if(extras.containsKey(Constants.DISPLAY_ROTD))
		{
			new ViewRecipesAsyncWebCall(this, ViewRecipeActivity.this).execute(WebServiceUrls.VIEW_ROTD);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return true;
	}
	
	/**
	 * Returns recipe associated with this activity
	 * @return the recipe
	 */
	public Recipe getAssociatedRecipe() {
		return associatedRecipe;
	}

	/**
	 * Sets the recipe that should be associated & shown in this activity
	 * @param associatedRecipe
	 */
	public void setAssociatedRecipe(Recipe associatedRecipe) {
		this.associatedRecipe = associatedRecipe;
		
		/* Fill controls with data from recipe */
		recipeName.setText(associatedRecipe.getName());
		ingredients.setText(StringUtils.join(associatedRecipe.getIngredients(), "\n"));
		preparation.setText(associatedRecipe.getCookingInstruction());
		isVegetarian.setText(associatedRecipe.isVegetarian() ? 
				getResources().getString(R.string.vegetarian) :
				getResources().getString(R.string.not_vegetarian));
		
		/* Load associated image into ImageView */
		new GetImageWebCall(image).execute(associatedRecipe.getImageFilePath(), String.valueOf(false));
	}

	/* Events of ViewRecipeAsyncWebCall */
	
	@Override
	public void addRecipes(List<Recipe> recipes) {
		Recipe recipe = recipes.get(0);
		setAssociatedRecipe(recipe);
	}

	@Override
	public void webServiceCallFailed(Exception e) {
		Toast.makeText(this, e.getMessage() , Toast.LENGTH_LONG).show();
		
		Intent i = new Intent(this, MainActivity.class);
		startActivity(i);
	}
	
}
